<template>
  <van-form @submit="onSubmit">
    <van-cell-group inset>
      <van-field
          v-model="editUser.currentValue"
          :name="editUser.editName"
          :label="editUser.editKey"
          :placeholder="'请修改 ' + editUser.editKey"
      />

    </van-cell-group>
    <div style="margin: 16px;">
      <van-button round block type="primary" native-type="submit">
        提交
      </van-button>
    </div>
  </van-form>


</template>
<script setup lang="ts">
import {  reactive, watchEffect } from 'vue';
import { useRoute, useRouter } from 'vue-router';
import { showSuccessToast, showFailToast } from 'vant';
import myAxios from "../plugins/myAxios";
import {getCurrentUser} from "../services/user";

const router = useRouter();
const route = useRoute();

// 使用 reactive 而不是 ref，来存储 editUser 对象
const editUser = reactive({
  editKey: '',
  editName: '',
  currentValue: ''
});

// 监听路由变化并更新 editUser
watchEffect(() => {
  if (route.query.editKey && route.query.editName && route.query.currentValue) {
    editUser.editKey = route.query.editKey;
    editUser.editName = route.query.editName;
    editUser.currentValue = route.query.currentValue;
  }
});



// 提交表单的处理函数
 const  onSubmit = async() => {
   //获取当前登录用户信息
   const currentUser =await  getCurrentUser();

   if(!currentUser){
     showSuccessToast('用户未登录');
     return;
   }




  const res: any = await myAxios.post('/user/update',{
    'id' : currentUser.id,
    [editUser.editKey] :editUser.currentValue,
  })
  console.log(res,"更新请求");
  if (res.code === 0 && res.data>0){
    showSuccessToast("更新请求成功");
    router.back();
  }else {
    showFailToast("更新请求失败");
  }

};

</script>


<style scoped>

</style>